package com.sitc.administrator.application.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;
import com.sitc.administrator.application.R;
import com.sitc.administrator.application.domain.LoginRes;
import com.sitc.administrator.application.domain.User;
import com.sitc.administrator.application.domain.event.LoginSuccessEvent;
import com.sitc.administrator.application.network.Api;
import com.sitc.administrator.application.network.reactivex.HttpListener;
import com.sitc.administrator.application.util.ToastUtil;

import org.apache.commons.lang3.StringUtils;
import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import static com.sitc.administrator.application.util.Md5Util.getMD5Str;


public class LoginActivity extends BaseCommonActivity {


    private static final String TAG = "LoginActivity";
    private Map<String, String> params;
    @BindView(R.id.et_user)
    EditText et_user;

    @BindView(R.id.et_password)
    EditText et_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        onTheme();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.bt_login)
    public void bt_login() {

        String user_name = et_user.getText().toString();
        if (StringUtils.isEmpty(user_name)) {
            ToastUtil.showSortToast(getActivity(), R.string.enter_user);
            return;
        }

        //String password = "e10adc3949ba59abbe56e057f20f883e";
        String password = et_password.getText().toString();
        if (StringUtils.isEmpty(password)) {
            ToastUtil.showSortToast(getActivity(), R.string.enter_password);
            return;
        }

        String _password = getMD5Str(password);


        params = new HashMap<String, String>();
        params.put("username", user_name);
        params.put("password", _password);
        params.put("sysId", "SITCAPP");

        Api.getInstance().login(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpListener<LoginRes>(getActivity()) {
                    @Override
                    public void onSucceeded(LoginRes data) {
                        super.onSucceeded(data);

                        if(data.getToken()!=null || !TextUtils.isEmpty(data.getToken())){

                            sp.setUserId(data.getAccountDetail().getAccountId());
                            sp.setUserName(params.get("username"));
                            sp.setToken(data.getToken());
                            sp.setPassword(params.get("password"));
                            sp.setDescription(data.getAccountDetail().getRoleName());

                            startActivityAfterFinishThis(MainActivity.class);


                        }else {

                            ToastUtil.showSortToast(getActivity(), R.string.login_erro);
                        }

                    }
                });

        }





}

